using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckOut : System.Web.UI.UserControl
{
    processOrder pOrder;
    processOrderDetail pOrderDetail;
    processPaymentType pPayment;
    processDeliveryType pDelivery;
    processUser pUser;
    protected void Page_Load(object sender, EventArgs e)
    {
        
            pOrder = new processOrder();
            pOrderDetail = new processOrderDetail();
            pPayment = new processPaymentType();
            pDelivery = new processDeliveryType();
            pUser = new processUser();

            if (Session["UserName"] == null)
            {
                Response.Redirect("index.aspx?subpara=login");
            }
            string UserName = Session["UserName"].ToString();
            string Password = Session["Password"].ToString();
            string UserID = pUser.getUserID1(UserName, Password);
           
            Load_gvwCart(UserID);
            Load_DataddlPayment();
            Load_DataddlDelivery();
            btnUpdate.Visible = false;
            btnRemoveOrder.Visible = false;

            
    }

    public void Load_gvwCart(string UserID)
    {
        DataTable dtProduct = pOrder.getOrderByUserID(UserID);
        if (dtProduct == null)
        {
            lblResult.Text = "Has no product in your cart!";
        }
        else
        {
            gvwCart.DataSource = dtProduct.DefaultView;
            gvwCart.DataBind();
        }
        
    }   

    private void Load_DataddlPayment()
    {
        ddlPayment.DataSource = pPayment.getPaymentType();
        ddlPayment.DataTextField = "PaymentTypeName";
        ddlPayment.DataValueField = "PaymentTypeID";
        ddlPayment.DataBind();
    }

    private void Load_DataddlDelivery()
    {
        ddlDelivery.DataSource = pDelivery.getDeliveryType();
        ddlDelivery.DataTextField = "DeliveryTypeName";
        ddlDelivery.DataValueField = "DeliveryTypeID";
        ddlDelivery.DataBind();
    }

    private void Load_DataControl(string ProductID)
    {        
           
            DataTable dtProduct = pOrder.getOrderByProductID(ProductID);
            for (int i = 0; i < dtProduct.Rows.Count; i++)
            {
                txtOrderDate.Text = dtProduct.Rows[i]["Orderdate"].ToString();
                txtReceiverName.Text = dtProduct.Rows[i]["ReceiverName"].ToString();
                txtAddress.Text = dtProduct.Rows[i]["OrderAddress"].ToString();
                txtOrderStatus.Text = dtProduct.Rows[i]["OrderStatus"].ToString();
                ddlDelivery.SelectedValue = dtProduct.Rows[i]["DeliveryTypeID"].ToString();
                ddlPayment.SelectedValue = dtProduct.Rows[i]["PaymentTypeID"].ToString();
            }
            viewButton();
    }

    protected bool viewButton()
    {
        DateTime dateNow = DateTime.Now;
        DateTime dateOrdder = Convert.ToDateTime(txtOrderDate.Text);
        DateTime dateUpdate = dateOrdder.AddDays(7);

        //if customer's order is delivered, he/she can not update his/her order
        if (txtOrderStatus.Text.Equals("Delivered"))
        {
            btnUpdate.Visible = false;
            btnRemoveOrder.Visible = false;
            txtOrderDate.ReadOnly = true;
            txtReceiverName.ReadOnly = true;
            txtAddress.ReadOnly = true;
            ddlDelivery.Enabled = false;
            ddlPayment.Enabled = false;
            txtOrderStatus.ReadOnly = true;
            return true;
        }

        //If after 7 days, customer can not update his/her order
        else if (dateNow.CompareTo(dateUpdate)>0)
        {
            btnUpdate.Visible = false;
            btnRemoveOrder.Visible = false;
            txtOrderDate.ReadOnly = true;
            txtReceiverName.ReadOnly = true;
            txtAddress.ReadOnly = true;
            ddlDelivery.Enabled = false;
            ddlPayment.Enabled = false;
            txtOrderStatus.ReadOnly = true;
            return true;
        }
        else
        {
            btnUpdate.Visible = true;
            btnRemoveOrder.Visible = true;
            txtOrderDate.ReadOnly = true;
            txtReceiverName.ReadOnly = false;
            txtAddress.ReadOnly = false;
            ddlDelivery.Enabled = true;
            ddlPayment.Enabled = true;
            txtOrderStatus.ReadOnly = true;
            return false;
        }
    }

    protected void gvwCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            Label lblQuantity = (Label)e.Row.FindControl("lblQuantity");
            lblQuantity.Text = ((DataRowView)e.Row.DataItem)["Quantity"].ToString();
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = ((DataRowView)e.Row.DataItem)["Price"].ToString();            
        }
    }
    protected void gvwCart_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string UserName = Session["UserName"].ToString();
        string Password = Session["Password"].ToString();
        string UserID = pUser.getUserID1(UserName, Password);
        gvwCart.PageIndex = e.NewPageIndex;
        txtReceiverName.Text = "";
        txtOrderDate.Text = "";
        txtAddress.Text = "";
        txtOrderStatus.Text = "";
        btnRemoveOrder.Visible = false;
        btnUpdate.Visible = false;
        ddlPayment.ClearSelection();
        ddlDelivery.ClearSelection();
        Load_gvwCart(UserID);        
    }
    protected void gvwCart_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {         
        string UserName = Session["UserName"].ToString();
        string Password = Session["Password"].ToString();
        string UserID = pUser.getUserID1(UserName, Password);
        string ProductID = gvwCart.DataKeys[e.NewSelectedIndex].Value.ToString();
        txtProductID.Text = ProductID;
        Load_DataControl(ProductID);
        Load_gvwCart(UserID);
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtOrderStatus.Text.Equals("Delivered"))
            {
                btnUpdate.Visible = false;
                btnRemoveOrder.Visible = false;
            }
            else
            {
                string UserName = Session["UserName"].ToString();
                string Password = Session["Password"].ToString();
                string UserID = pUser.getUserID1(UserName, Password);
                string Delivery = ddlDelivery.SelectedValue.ToString();
                string Payment = ddlPayment.SelectedValue.ToString();                               
                string ProductID = txtProductID.Text;
                string OrderID = pOrder.getOrderIDByUserandProduct(UserID, ProductID);

                string ReceiverName = txtReceiverName.Text;
                string Address = txtAddress.Text;                

                if (pOrder.updateOrder(OrderID, ReceiverName, Address, Payment, Delivery) > 0)                
                {
                    Alert.Show("Update Order successfull!");
                    Load_DataControl(ProductID);
                }
                else
                    Alert.Show("Update Order unsuccessfull!");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<br />" + ex.StackTrace);
        }
    }
    protected void btnRemoveOrder_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtOrderStatus.Text.Equals("Delivered"))
            {
                btnUpdate.Visible = false;
                btnRemoveOrder.Visible = false;
            }
            else
            {
                string UserName = Session["UserName"].ToString();
                string Password = Session["Password"].ToString();
                string UserID = pUser.getUserID1(UserName, Password);
                DataTable dtOrderDetail = pOrder.getOrderByUserID(UserID);
                for (int i = 0; i < dtOrderDetail.Rows.Count; i++)
                {
                    string OrderNumber = dtOrderDetail.Rows[i]["OrderNumber"].ToString();
                    string ProductID = txtProductID.Text;
                    string OrderID = dtOrderDetail.Rows[i]["OrderID"].ToString();
                    if (pOrderDetail.deleteOrderDetail(ProductID,OrderID) > 0)
                    {
                        Alert.Show("Delete Order Detail successfull!");
                        Load_gvwCart(UserID);
                    }
                    if (OrderID == null)
                    {
                        if (pOrder.deleteOrder(OrderID) > 0)
                        {
                            Alert.Show("Delete Order successfull!");
                        }

                    }
                }
                

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<br />" + ex.StackTrace);
        }
    }
}
